
package com.cx.cxapi;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

import com.jws.thirdlibrary.CxManager;


public class GpioActivity extends Activity {
    private Button gpio_on;
    private Button gpio_off;
    private Button gpio_read;
    private Spinner gpioPort;
    private CxManager mCxManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gpio);
        initSource();
        onClickVersion();
    }

	@Override
	protected void onResume() {
		super.onResume();
		mCxManager = CxManager.create(this);
	}

    private void initSource() {
        gpioPort = (Spinner) findViewById(R.id.gpio_port);
        gpio_on = (Button) findViewById(R.id.btn_gpio_on);
        gpio_off = (Button) findViewById(R.id.btn_gpio_off);
        gpio_read = (Button) findViewById(R.id.btn_gpio_read);
    }


    private void onClickVersion() {
        gpio_on.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int port = gpioPort.getSelectedItemPosition();
				mCxManager.jwsSetExtrnalGpioValue(port, true);
                Toast.makeText(getApplicationContext(), getString(R.string.ftm_gpio_out_open), Toast.LENGTH_SHORT).show();
            }
        });
        gpio_off.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                int port = gpioPort.getSelectedItemPosition();
				mCxManager.jwsSetExtrnalGpioValue(port, false);
                Toast.makeText(getApplicationContext(), getString(R.string.ftm_gpio_out_close), Toast.LENGTH_SHORT).show();
            }
        });

        gpio_read.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                int port = gpioPort.getSelectedItemPosition();
                int value = mCxManager.jwsReadInputGpioValue(port);
                String Toaststr = "Gpio" + String.valueOf(port) + "==" + String.valueOf(value);
                Toast.makeText(getApplicationContext(), Toaststr, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
